package com.gomtel.smartdevice.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;


import com.gomtel.smartdevice.di.component.DaggerFragmentComponent;
import com.gomtel.smartdevice.di.component.FragmentComponent;
import com.gomtel.smartdevice.di.moudle.FragmentModule;
import com.gomtel.smartdevice.global.WatchApplication;
import com.gomtel.smartdevice.moudle.DataManager;
import com.gomtel.smartdevice.moudle.event.CommonEvent;
import com.gomtel.smartdevice.moudle.rxtools.EventBusUtil;
import com.gomtel.smartdevice.utils.ToastUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import javax.inject.Inject;

/**
 * Created by Administrator on 2018/3/19.
 */

public abstract class WatchBaseFragemnt extends SimpleFragment {

    @Inject
    public ToastUtil toastUtil;

    @Inject
    public DataManager dataManager;

    protected FragmentComponent getFragmentComponent() {
        return DaggerFragmentComponent.builder()
                .appComponent(WatchApplication.getAppComponent())
                .fragmentModule(getFragmentModule())
                .build();
    }

    protected FragmentModule getFragmentModule() {
        return new FragmentModule(this);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        initInject();
        EventBusUtil.register(this);
        super.onViewCreated(view, savedInstanceState);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBusCome(CommonEvent event) {
        if (event != null) {
            registerEvent(event);
        }
    }

    protected void receiveStickyEvent(CommonEvent event) {

    }

    protected abstract void initInject();

    protected abstract void registerEvent(CommonEvent commonevent);

    @Override
    protected void onUnSubscribe() {
        super.onUnSubscribe();
        EventBusUtil.unregister(this);
    }
}
